import { OrderType } from '@/enums'
import { cancelOrder, deleteOrder, followOrUnfollow, getPresctiptionPic } from '@/services/consult'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import { showFailToast, showImagePreview, showSuccessToast, showToast } from 'vant'
import type { OrderDetail } from '@/types/order'
import { getMedicalOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import { ref, onMounted,onUnmounted } from 'vue'
import type { CodeType } from '@/types/user'
import type { FormInstance } from 'vant'
import type { Ref } from 'vue'

//Vue3概念：通过组合式API封装数据逻辑在一起的函数，组合式函数，以use开头useXxx
//放在composable文件夹下，composable就是组合式函数
export const useFollow = (type: FollowType = 'doc') => {
    const loading = ref(false)
    const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
      loading.value = true
      try {
        await followOrUnfollow(item.id, type)
        item.likeFlag = item.likeFlag === 1 ? 0 : 1
      } finally {
        loading.value = false
      }
  }
  return { loading, follow }
  }

//提供查看处方
export const useShowPrescription = () => {
  const onShowPrescription = async (id?: string) => {
    if (id) {
      const res = await getPresctiptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return { onShowPrescription }
}

//取消订单
export const useCancelOrder = () => {
const loading = ref(false)
const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
        loading.value = true
        await cancelOrder(item.id)
        item.status = OrderType.ConsultCancel
        item.statusValue = '已取消'
        showSuccessToast('取消成功')
    } catch (error) {
        showFailToast('取消失败')
    } finally {
        loading.value = false
    }
}
return { loading, cancelConsultOrder }
}

//删除订单
export const useDeleteOrder = (cb: () => void) => {
  const loading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
      try {
          loading.value = true
          await deleteOrder(item.id)
          showSuccessToast('删除成功')
          cb && cb()
      } catch (error) {
          showFailToast('删除失败')
      } finally {
          loading.value = false
      }
  }
  return { loading, deleteConsultOrder }
}

//药品订单详情
export const useOrderDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  onMounted(async () => {
    try {
      loading.value = true
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { loading, order }
}

//发送短信验证码
export const useMobileCode = (mobile: Ref<string>, type: CodeType = 'login') => {
  const time = ref(0)
  /* const form = ref<InstanceType<typeof Form>>() */
  const form = ref<FormInstance>()
  let timer: number
  const onSend = async () => {
      //验证：倒计时 手机号
      if (time.value > 0) return
      await form.value?.validate('mobile')
      // console.log('send')
      await sendMobileCode(mobile.value, type)
      showToast('发送成功')
      time.value = 60
      //开启倒计时
      if (timer) clearInterval(timer)
      timer = setInterval(() => {
          time.value--
          if (time.value <= 0) clearInterval(timer)
      }, 1000)
  }
  onUnmounted(() => {
    clearInterval(timer)
  });
  return { onSend, time, form  }
}

